package com.abso.sunlight.explorer.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.services.IEvaluationService;

import com.abso.sunlight.api.LegislatorQuery;
import com.abso.sunlight.api.SunlightException;
import com.abso.sunlight.explorer.CongressExplorerPlugin;
import com.abso.sunlight.explorer.LegislatorSearch;
import com.abso.sunlight.explorer.dialogs.SaveLegislatorSearchDialog;
import com.abso.sunlight.explorer.editors.LegislatorListEditor;

/**
 * Saves a search with a new name.
 */
public class SaveAsLegislatorSearchHandler extends AbstractHandler {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
        if (activeEditor instanceof LegislatorListEditor) {
            LegislatorListEditor legislatorListEditor = ((LegislatorListEditor) activeEditor);
            LegislatorSearch search = legislatorListEditor.getSearch();
            SaveLegislatorSearchDialog saveDialog = new SaveLegislatorSearchDialog(HandlerUtil.getActiveShell(event), search.getName());
            if (saveDialog.open() == Window.OK) {
                try {
                    LegislatorSearch newSearch = search;
                    if (search.isSaved()) {

                        /* clones the search */
                        newSearch = new LegislatorSearch();
                        LegislatorQuery newQuery = new LegislatorQuery();
                        newQuery.loadFromXML(search.getQuery().toXML().getRootElement());
                        newSearch.setQuery(newQuery);
                        newSearch.setResult(search.getResult());
                    }
                    newSearch.setName(saveDialog.getSearchName());
                    CongressExplorerPlugin.getSearchManager().addSearch(newSearch, search);
                    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
                    if (window != null) {
                        IEvaluationService evService = (IEvaluationService) window.getService(IEvaluationService.class);
                        evService.requestEvaluation("com.abso.sunlight.explorer.savedSearch");
                    }
                } catch (SunlightException e) {
                    ExecutionException e2 = new ExecutionException("Unable to save", e);
                    ErrorDialog.openError(HandlerUtil.getActiveShell(event), "Error", e2.getMessage(), new Status(IStatus.ERROR,
                            CongressExplorerPlugin.PLUGIN_ID, e.getMessage(), e));
                    throw e2;
                }
            }
        }
        return null;
    }

}
